import 'package:app/common_widget/colors.dart';
import 'package:app/common_widget/hexcolor.dart';
import 'package:app/home_module/app_grid_model.dart';
import 'package:app/net/address.dart';
import 'package:app/net/dio_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../common_widget/top_dialog_page.dart';
import '../net/persisten_storage.dart';
import '../qrcode_module/qrcode_page.dart';
import 'all_module_page.dart';
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {


  AppGridModel? _model;

  var companyName = '';
  void getInitData()async{
    companyName = await PersistentStorage().getStorage('companyIdName');
    setState(() {

    });
  }


  requestData()async{

    var json = await DioManager().kkRequest(Address.appGridIndex,method: 'get');

    AppGridModel model = AppGridModel.fromJson(json);

    _model = model;

    setState(() {

    });

  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getInitData();
    requestData();
  }
  
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

          centerTitle: false,
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              GestureDetector(
                child:   Row(
                  children: [
                    Container(
                      // color: Colors.yellow,
                      child: Text('${companyName??''}',
                        style: TextStyle(color: HexColor('#232323'),fontSize: 20),),
                    ),
                    Container(
                      padding: EdgeInsets.only(top: 5),
                      // color: Colors.red,
                      child: Image.asset('images/downarr_icon.png',width: 24,height: 24,),
                    ),
                  ],
                ),
                onTap:(){
                  Get.dialog(TopDiaLogPage(),barrierColor: Colors.transparent);
                } ,
              ),
              GestureDetector(
                child:  Container(
                  child: Image.asset('images/qrcode_icon.png',width: 24,height: 24,),
                ),
                onTap: (){
                  Get.to(QRCodePage());
                },
              )
            ],
          ),

      ),
      backgroundColor: AppColor.bgColor,
      body: EasyRefresh.custom(
        header: MaterialHeader(),
        onRefresh: ()async{
          requestData();
        },
        slivers: [
          SliverToBoxAdapter(
            child: Container(
              margin: EdgeInsets.only(top: 15,left: 15,right: 15),
              child: Image.asset('images/top_bg_icon.png'),
            ),
          ),

          SliverList(delegate: _mySliverChildBuilderDelegate(),),
        ],
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
        return Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),
          margin: EdgeInsets.only(top: 10,left: 15,right: 15),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                // color: Colors.red,
                  width: Get.width-15,
                  padding: EdgeInsets.only(top: 10,left: 10,right: 10),
                  child:Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('${_model?.data?[index].categoryName}',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 16),),
                      GestureDetector(
                        onTap: (){
                          print('qb');
                          Get.to(AllModulePage());
                        },
                        child:Row(
                          children: [
                            Text('全部',style: TextStyle(color: AppColor.smallTextColor,fontSize: 12
                            ),),
                            Icon(Icons.arrow_forward_ios_outlined,size: 11,color: AppColor.smallTextColor,)
                          ],
                        ),
                      )
                    ],
                  )
              ),
              SizedBox(height: 15,),
              GridView.builder(
                physics: const NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                gridDelegate: _mySliverGridDelegateWithFixedCrossAxisCount(),
                itemBuilder: (BuildContext context, int a) {
                  return Container(
                    height: 50,
                    margin: EdgeInsets.only(top: 5,left: 10),
                    // color: Colors.orange,
                    // color: Colors.primaries[index % 8],
                    child: Column(
                      children: [
                        Image.asset('images/demo_icon.png'),
                        Text('${_model?.data?[index].apps?[a].name}',style: TextStyle(fontSize: 12),)
                      ],
                    ),
                  );
                },itemCount: _model?.data?[index].apps?.length??0,),
            ],
          ),
        );
      },
      childCount: _model?.data?.length??0,
    );
  }

  _mySliverGridDelegateWithFixedCrossAxisCount() {
    return SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 5,
      mainAxisSpacing: 10,
      crossAxisSpacing: 5,
      childAspectRatio: 1/1.2,
    );
  }
}
